package cmd

import (
	"gallery/crawler"
	"math"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:     "gallery",
	Short:   "gallery 是一个FA画廊爬虫。",
	Long:    "FA Gallery爬虫，速度快，操作简单",
	Version: "1.6.3",
	// 为所有命令添加前置处理
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		crawler.Duration = int(math.Abs(float64(crawler.Duration)))
		crawler.GetClient()
	},
}

func init() {
	// 设置代理参数
	rootCmd.PersistentFlags().StringVarP(&crawler.X, "proxy", "x", "", "配置代理")

	// 设置命令行参数
	rootCmd.PersistentFlags().IntVarP(&crawler.Page, "page", "p", 0, "获取第p页，默认全部")
	rootCmd.PersistentFlags().StringVarP(&crawler.Dir, "outdir", "o", "./pics/", "目标文件夹，需要带最后的斜杠")
	rootCmd.PersistentFlags().StringVarP(&crawler.Cookie, "cookie", "b", "", "指定cookie用来模拟登录")
	rootCmd.PersistentFlags().IntVarP(&crawler.Duration, "duration", "t", 1, "间隔时间，防止并发量过大(单位：s)")
}

// Execute 启动命令行程序
func Execute() {
	if err := rootCmd.Execute(); err != nil {
		//log.Fatalln(err)
		os.Exit(1)
	}
}
